package com.cpecc.manager.wuzi;

import com.cpecc.dao.wuzi.BaoFeiDao;
import com.cpecc.liucheng.manager.HuanJieManager;
import com.cpecc.manager.system.ShiYongZTManager;
import com.cpecc.manager.system.UserManager;
import com.cpecc.model.system.User;
import com.cpecc.model.wuzi.BaoFei;
import com.cpecc.model.wuzi.BaoFeiJL;
import com.cpecc.model.wuzi.BaoFeiSPR;
import com.cpecc.model.wuzi.WuZi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class BaoFeiManager {

	@Autowired
	private BaoFeiDao baoFeiDao;

	@Autowired
	private HuanJieManager huanJieManager;

	@Autowired
	private BaoFeiJLManager baoFeiJLManager;

	@Autowired
	private BaoFeiSPRManager baoFeiSPRManager;

	@Autowired
	private WuZiManager wuZiManager;

	@Autowired
	private UserManager userManager;

	@Autowired
	private ShiYongZTManager shiYongZTManager;
	
	public void save(BaoFei baoFei){
		baoFeiDao.save(baoFei);
	}
	
	public BaoFei get(String id){
		return baoFeiDao.get(id);
	}
	
	public boolean cover(BaoFei baoFei){
		return baoFeiDao.cover(baoFei);
	}
	
	public List<BaoFei> getMyBaoFei(User user){
		return baoFeiDao.getMyBaoFei(user);
	}
	
	public List<BaoFei> getBaoFeiList(int huanJie){
		return baoFeiDao.getBaoFeiList(huanJie);
	}

	/**
	 * 报废使用人确认列表
	 * @param user
	 * @return
	 */
	public List<BaoFei> getBfsysqrList(User user){
		return baoFeiDao.getBfsyrqrList(user);
	}

	public List<BaoFei> getBaoFeiList(int huanJie,User user){
		return baoFeiDao.getBaoFeiList(huanJie,user);
	}
	
	public List<BaoFei> getBaoFeiNeiGouList(int huanJie,User user){
		return baoFeiDao.getBaoFeiNeiGouList(huanJie,user);
	}
	
	public List<BaoFei> getBaoFeiBMList(User user){
		return baoFeiDao.getBaoFeiBMList(user);
	}
	
	public List<BaoFei> getAllBaoFeiKu(WuZi wuZi,String wuZiLBId){
		return baoFeiDao.getAllBaoFeiKu(wuZi, wuZiLBId);
	}
	
	public List<BaoFei> getAllByWuZi(WuZi wuZi){
		return baoFeiDao.getAllByWuZi(wuZi);				
	}
	
	public boolean hasWuZi(String wuZiId){
		return baoFeiDao.hasWuZi(wuZiId);
	}
	
	public boolean hasLiuCheng(User user){
		return baoFeiDao.hasLiuCheng(user);
	}
	
	public List<BaoFei> activeBaoFei(){
		return baoFeiDao.activeBaoFei();
	}

	public void submit(BaoFei baoFei, User currentUser) {
		baoFei.setHuanJie(huanJieManager.get(702));
		save(baoFei);

		WuZi wuZi = baoFei.getWuZi();
		wuZi.setShiYongZT(shiYongZTManager.get(106));
		wuZiManager.cover(wuZi);

		jiLu(baoFei, 701, "提交报废申请", currentUser);
		addSpr(baoFei, 702, baoFei.getUser());
	}

	public void check(String[] baoFeiId, int nextHunanJie, String content, User currentUser) {
		if (baoFeiId == null || baoFeiId.length <= 0) {
			return;
		}

		BaoFei baoFei = null;
		for (String bfId : baoFeiId) {
			baoFei = get(bfId);
			if (baoFei == null) {
				continue;
			}

			int currentHuanJie = baoFei.getHuanJie().getId();

			baoFei.setHuanJie(huanJieManager.get(nextHunanJie));
			cover(baoFei);


			if (nextHunanJie == 718) {
				WuZi wuZi = baoFei.getWuZi();
				wuZi.setShiYongZT(shiYongZTManager.get(102));
				wuZiManager.cover(wuZi);
			} else if (nextHunanJie == 713) {
				WuZi wuZi = baoFei.getWuZi();
				wuZi.setShiYongZT(shiYongZTManager.get(107));
				wuZiManager.cover(wuZi);
			}

			jiLu(baoFei, currentHuanJie, content, currentUser);
			addSpr(baoFei, nextHunanJie, baoFei.getUser());
		}
	}

	private void jiLu(BaoFei baoFei, int huanJie, String content, User user) {
		BaoFeiJL baoFeiJL=new BaoFeiJL();
		baoFeiJL.setBaoFei(baoFei);
		baoFeiJL.setCaoZuoRQ(new Date());
		baoFeiJL.setHuanJie(huanJieManager.get(huanJie));
		baoFeiJL.setPiFuNR(content);
		baoFeiJL.setUser(user);
		baoFeiJLManager.save(baoFeiJL);
	}

	private void addSpr(BaoFei baoFei, int huanJie, User user) {
		baoFeiSPRManager.deleteAllSPR(baoFei.getId());
		List<User> sprList = getSPR(huanJie, user);
		if (!CollectionUtils.isEmpty(sprList)) {
			BaoFeiSPR baoFeiSPR = null;
			for (User spe : sprList) {
				baoFeiSPR = new BaoFeiSPR();
				baoFeiSPR.setBaoFei(baoFei);
				baoFeiSPR.setNo(1);
				baoFeiSPR.setUser(spe);
				baoFeiSPRManager.save(baoFeiSPR);
			}
		}
	}

	private List<User> getSPR(int huanJie, User user) {
		List<User> list = null;
		switch(huanJie){
			// 报废部门审批、管理员不同意
			case 702:
			case 723:
				list = userManager.getBMFZR(user);
				break;

			//管理员审批、归口部门负责人不同意
			case 703:
			case 727:
				list = userManager.getGZGLY(); break;

			// 归口部门负责人审批
			case 717:
				list = userManager.getGKBMFZR(); break;
			default: break;
		}
		return list;
	}
}
